package com.apinji.common.plugin;

import com.apinji.interceptor.SignInterceptor;
import com.apinji.unionpay.sdk.HttpClient;
import com.apinji.unionpay.sdk.LogUtil;
import com.apinji.unionpay.sdk.SDKUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by 轩辕夏夏 on 2017/7/6.
 */
public final class UnionpayUtils {
    private UnionpayUtils(){}

    public static String getOutOrderId() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }


    /**
     * 功能：后台交易提交请求报文并接收同步应答报文<br>
     * @param reqData 请求报文<br>
     * @param reqUrl  请求地址<br>
     * @param encoding<br>
     * @return 应答http 200返回true ,其他false<br>
     */
    public static Map<String,String> post(Map<String, String> reqData, String reqUrl, String encoding) {
        Map<String, String> rspData = new HashMap<String,String>();
        LogUtil.writeLog("请求地址:" + reqUrl);
        //发送后台请求数据
        HttpClient hc = new HttpClient(reqUrl, 30000, 30000);
        try {

            String timestamp = new Date().getTime()+"";
            String key = UUID.randomUUID().toString();
            String lastKey = key + SignInterceptor.PRIVATE_KEY + timestamp;
            String mySign = MD5Utils.MD5To32(lastKey);

            Map<String, String> reqHeaders = new HashMap<String, String>();
            reqHeaders.put("timestamp", timestamp);
            reqHeaders.put("key", key);
            reqHeaders.put("sign", mySign);

            int status = hc.send(reqData, reqHeaders, encoding);
            if (200 == status) {
                String resultString = hc.getResult();
                if (null != resultString && !"".equals(resultString)) {
                    // 将返回结果转换为map
                    Map<String,String> tmpRspData  = SDKUtil.convertResultStringToMap(resultString);
                    rspData.putAll(tmpRspData);
                }
            }else{
                LogUtil.writeLog("返回http状态码["+status+"]，请检查请求报文或者请求地址是否正确");
            }
            rspData.put("rspStatus", status+"");
        } catch (Exception e) {
            LogUtil.writeErrorLog(e.getMessage(), e);
        }
        return rspData;
    }


    /**
     * 获取请求参数中所有的信息
     *
     * @param request
     * @return
     */
    public static Map<String, String> getAllRequestParam(final HttpServletRequest request) {
        Map<String, String> res = new HashMap<String, String>();
        Enumeration<?> temp = request.getParameterNames();
        if (null != temp) {
            while (temp.hasMoreElements()) {
                String en = (String) temp.nextElement();
                String value = request.getParameter(en);
                res.put(en, value);
                //在报文上送时，如果字段的值为空，则不上送<下面的处理为在获取所有参数数据时，判断若值为空，则删除这个字段>
                //System.out.println("ServletUtil类247行  temp数据的键=="+en+"     值==="+value);
                if (null == res.get(en) || "".equals(res.get(en))) {
                    res.remove(en);
                }
            }
        }
        return res;
    }

}
